package com.wn.sk.product.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.wn.common.api.Result;
import com.wn.sk.product.contants.PromotionStatus;
import com.wn.sk.product.entity.Product;
import com.wn.sk.product.entity.vo.PromotionOrderVo;
import com.wn.sk.product.entity.vo.PromotionProductVo;
import com.wn.sk.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2022-11-07
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品模块")
public class ProductController {

    @Autowired
    private IProductService productService;

    @ApiOperation("产品查询所有")
    @GetMapping("find")
    public Result<List<Product>> findProducts(){
        List<Product> list = productService.list();
        return Result.ok(list);
    }


    @ApiOperation("查询秒杀商品")
    @GetMapping("/sk")
    public Result<List<PromotionProductVo>> findPromotion() throws JsonProcessingException {

        //查询互动已开始的商品
        List<PromotionProductVo> productVos = productService.findPromotionProduct(PromotionStatus.START);

        return Result.ok(productVos);
    }


    @ApiOperation("产品查询,有权限操作")
    @GetMapping("/auth/find")
    public Result<String> findAuthProducts(){

        return Result.ok("auth 有权限就可以访问    product微服务");
    }

    /**
     * 获取前端拿到的数据:
     *
     * 1、@RequestParam 获取请求参数
     * 2、对象绑定
     * 3、@PathVariable  获取restful的数据
     * 4、获取头信息  @RequestHeader
     * 5、springmvc中 可以注入javaweb的组件 获取请求数据
     *
     * @param promotionId
     * @return
     */
    @ApiOperation("秒杀活动下单")
    @GetMapping("/auth/order/{promotionId}")
    public Result<PromotionOrderVo> submitOrder(@PathVariable("promotionId") Integer promotionId,
                                                HttpServletRequest httpServletRequest){

        String userId = httpServletRequest.getHeader("userId");
        //下一个订单
        PromotionOrderVo promotionOrderVo =  productService.submitOrder(promotionId,userId);
        return Result.ok(promotionOrderVo);
    }


}

